package com.fairytask.engine.components.list
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.ColorDefine;
	import com.fairytask.managers.ResManager;
	import com.fairytask.resource.StaticIni;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 列表组件
	 * 
	 * data格式:{label:"测试0",height:25,skin:"Button_5"}
	 * @author hanjy
	 */
	public class List extends BaseComponent
	{
		//容器
		private var _bgContainer		:Sprite;
		private var _itemContainer		:Sprite;
		private var _scrollContainer	:Sprite;
		
		//皮肤
		private var _bgSkin		:String	=	"";
		private var _itemSkin		:String	=	"";
		
		//设定
		private var _isScroll		:Boolean;
		private var _left			:Number	=	0;
		private var _top			:Number	=	0;
		private var _itemSpace		:Number	=	0;
		
		private var _fixedWidth	:Number	=	0;
		private var _fixedHeight	:Number	=	0;
		private var _layOut		:String =  "center";
		
		//滚动条
		private var _scrollPanel	:ScrollPanel;
		
		//选择背景
		private var _selectBg		:Shape	=	new Shape();
		
		//数据
		private var _listData		:Array	=	[];
		//是否需要选择背景
		private var _needSelectBg	:Boolean=	true;
		//自适应宽度
		private var _autoWidth		:Boolean	=false;
		private var _maxWidth		:Number;
		/**
		 * 
		 * @param w
		 * @param h
		 * @param useScrollPanel
		 * @param itemData
		 * @param backgroundSkin
		 */
		public function List(w:int,h:int,useScrollPanel:Boolean=true,itemData:Array=null,backgroundSkin:String="",itemSkinStr:String="",layOut:String="left")
		{
			super();
			_maxWidth	=	_fixedWidth		=	w;
			_fixedHeight	=	h;
			_itemSkin		=	itemSkinStr;
			_layOut			=	layOut;
			initGUI();
			
			listData		=	itemData;
			isScroll		=	useScrollPanel;
			
			bgSkin			=	backgroundSkin;
			
		}
		
		public function set layOut(val:String):void
		{
			_layOut = val;
		}
		
		public function get layOut():String
		{
			return _layOut;
		}
		
		private function initGUI():void
		{
			//开始画选择背景
			if(_itemSkin=="")
			{
				_selectBg.graphics.beginFill(uint("0x"+StaticIni.getIniVar("textColor",ColorDefine.GUIDEHIGHTLISHT)));
				_selectBg.graphics.drawRect(0,0,_fixedWidth,20);
				_selectBg.graphics.endFill();
			}
			else
			{
				_selectBg.graphics.beginFill(uint("0x"+StaticIni.getIniVar("textColor",ColorDefine.GUIDEHIGHTLISHT)),0);
			}
			
			//初始化容器
			_bgContainer	=	new Sprite();
			_itemContainer	=	new Sprite();
			_scrollContainer=	new Sprite();
			addChild(_scrollContainer);
			addChild(_bgContainer);
			addChild(_itemContainer);
			_scrollPanel	=	new ScrollPanel(new Rectangle(0,0,_fixedWidth,_fixedHeight),_scrollContainer,new ScrollSkin("vScrol_1","vScrol_5"));
		}
		private function drawBG(_h:Number):void
		{
			clearBG();
			if(_bgSkin!=""&&_bgSkin!=null)
			{
				var res:MovieClip	=	ResManager.newResClass(_bgSkin) as MovieClip;
				res.x	=	res.y	=	0;
//				res.width	=	_fixedWidth;
				res.width	=	_maxWidth;
				_h	=	_h==-1?_fixedHeight:_h;
				res.height	=	_h;
				_bgContainer.addChild(res);
			}
		}

		/**
		 * 
		 * @return 
		 */
		public function get isScroll():Boolean
		{
			return _isScroll;
		}

		/**
		 * 是否用滚动条
		 * @param value
		 */
		public function set isScroll(value:Boolean):void
		{
			_isScroll = value;
			if(_isScroll)
			{
				_scrollContainer.addChild(_bgContainer);
				_scrollContainer.addChild(_itemContainer);
				addChild(_scrollPanel);
				_scrollPanel.update();
			}else{
				addChild(_bgContainer);
				addChild(_itemContainer);
				if(_scrollPanel.parent!=null)
				{
					removeChild(_scrollPanel);
				}
			}
		}
		
		public function addObj(val:Object):void
		{
			addObjAt(val,_itemContainer.numChildren);
		}
		
		/**
		 * data格式:{label:"测试0",height:25,skin:"Button_5",data:"",left:10}
		 * @param val
		 */
		public function addObjAt(val:Object,index:int):void
		{
			if((index<0)	||	(index>_itemContainer.numChildren))
			{
				return;
			}
			var tmpItem:ListItem	=	new ListItem(val.label,_fixedWidth-left*2,val.height,getListSkin(val),val.data,_layOut);
			if(val.icon!=null)
			{
				var tmpIcon:Clip	=	new Clip();
				tmpIcon.source	=	val.icon;
				tmpItem.icon	=	tmpIcon;
			}
			_itemContainer.addChildAt(tmpItem,index);
			formatList();
		}
		
		/**
		 * 添加listItem对象 
		 * @param val
		 * 
		 */
		public function addItem(val:DisplayObject):void
		{
			_itemContainer.addChild(val);
			formatList();
		}
		
		private function formatList():void
		{
			if(_selectBg.parent!=null)
			{
				_selectBg.parent.removeChild(_selectBg);
			}
			var tmpHeight:Number	=	top;
			for(var i:int=0;i<_itemContainer.numChildren;i++)
			{
				var tmpItem:*	=	_itemContainer.getChildAt(i);
				if(tmpItem is ListItem)
				{
					tmpItem	=	tmpItem as ListItem;
					tmpItem.id	=	i;
					if(_itemSkin!="")
					{
						tmpItem.skin	=	_itemSkin;
					}
				}
				//自适应宽度，改变面板大小
				if(_autoWidth && tmpItem.width>_maxWidth)
				{
					_maxWidth	=	tmpItem.width;
					_scrollPanel.resize(new Rectangle(0,0,_maxWidth,_fixedHeight));
				}
				tmpItem.x	=	left;
				tmpItem.y	=	tmpHeight;
				tmpHeight	=	tmpHeight	+	tmpItem.height +itemSpace;
				if(!tmpItem.hasEventListener(MouseEvent.CLICK))
				{
					tmpItem.addEventListener(MouseEvent.CLICK,onItemClick);
				}
			}
			if(_isScroll)
			{
				_scrollPanel.update();
			}
			drawBG(tmpHeight);
		}
		
		/**
		 * 列表 
		 * 
		 */		
		public function txtLeft():void
		{
			for(var i:int=0;i<_itemContainer.numChildren;i++)
			{
				var tmpItem:ListItem	=	_itemContainer.getChildAt(i) as ListItem;
				tmpItem.alignLeft();
			}
		}
		
		/**
		 * 给listitem添加点击热区
		 * 
		 */		
		public function addHotArea():void
		{
			for(var i:int=0;i<_itemContainer.numChildren;i++)
			{
				var tmpItem:ListItem	=	_itemContainer.getChildAt(i) as ListItem;
				tmpItem.drawHotArea();
			}
		}
		
		private function onItemClick(evt:MouseEvent):void
		{
			_selectBg.y	=	evt.currentTarget.y;
			_selectBg.width	=	_maxWidth;
			if(_needSelectBg)
			{
				_itemContainer.addChildAt(_selectBg,0);
			}
			if(evt.currentTarget is ListItem)
			{
				(evt.currentTarget as ListItem).x = _left;
				dispatchEvent(new ListEvent(ListEvent.LIST_CLICK,evt.currentTarget as ListItem));
			}
		}
		
		/**
		 * 设置列表指定项被选中
		 * 
		 */		
		public function select(index:int):void
		{
			if(_selectBg.parent!=null)
			{
				_selectBg.parent.removeChild(_selectBg);
			}
			_selectBg.y = (20+_itemSpace)*index;
			if(_needSelectBg)
			{
				_itemContainer.addChildAt(_selectBg,0);
			}
//			var listItem		:ListItem	=	_itemContainer.getChildAt(index+1) as ListItem;
//			dispatchEvent(new ListEvent(ListEvent.LIST_CLICK,listItem));
			
		}
		private function clearItem():void
		{
			while(_itemContainer.numChildren>0)
			{
				_itemContainer.removeChildAt(0);
			}
		}
		
		/**
		 * 删除选择效果 
		 * 
		 */		
		public function clearSelectedBg():void
		{
			if(_selectBg.parent)
			{
				_selectBg.parent.removeChild(_selectBg);
			}
		}
		
		private function clearBG():void
		{
			while(_bgContainer.numChildren>0)
			{
				_bgContainer.removeChildAt(0); 
			}
		}
		
		/**
		 * 
		 * @param index
		 * @return 
		 */
		public function getItem(index:int):DisplayObject
		{
			var tmpItem:DisplayObject;
			if(index<_itemContainer.numChildren)
			{
				tmpItem	=	_itemContainer.getChildAt(index) as DisplayObject;
				if(tmpItem)
				{
					tmpItem.x = _left;
				}
			}
			return tmpItem;
		}
		/**
		 * 
		 * 
		 * 删除 
		 * 
		 */		
		public function removeItem(item:DisplayObject):void
		{
			item.parent.removeChild(item);
			//_itemContainer.removeChild(item);
			formatList();
		}
		/**
		 * 
		 * @param func
		 */
		public function getItemForEach(func:Function):void
		{
			for(var i:uint = 0 ; i < _itemContainer.numChildren ; i ++)
			{
				func.call(this,_itemContainer.getChildAt(i));
			}
		}
		/**
		 * 
		 * @return 
		 */
		public function get listData():Array
		{
			return _listData;
		}

		/**
		 * 传入以:data格式:{label:"测试0",height:25,skin:"Button_5"} 的数组
		 * @param value
		 */
		public function set listData(value:Array):void
		{
			clearItem();
			if(value==null)
			{
				return;
			}
			_listData = value;
			for(var i:int=0;i<_listData.length;i++)
			{
				var tmpObj	:Object		=	_listData[i];
				var tmpSkin	:String		=	_itemSkin;
				if(tmpObj["skin"]!=null)
				{
					tmpSkin	=	tmpObj["skin"];
				}
				var tmpItem:ListItem	=	new ListItem(tmpObj["label"],_fixedWidth-left*2,tmpObj["height"],tmpSkin,null);
				tmpItem.enabled			=	false;
				tmpItem.skin			=	tmpSkin;
				_itemContainer.addChild(tmpItem);
			}
			formatList();
		}

		private function getListSkin(val:Object):String
		{
			if(_itemSkin == ""){
				return val.skin;
			}
			return _itemSkin;
		}

		/**
		 * 
		 * @return 
		 */
		public function get top():Number
		{
			return _top;
		}

		/**
		 * 
		 * @param value
		 */
		public function set top(value:Number):void
		{
			_top = value;
		}

		/**
		 * 
		 * @return 
		 */
		public function get left():Number
		{
			return _left;
		}

		/**
		 * 
		 * @param value
		 */
		public function set left(value:Number):void
		{
			_left = value;
			formatList();
		}

		/**
		 * 
		 * @return 
		 */
		public function get itemSpace():Number
		{
			return _itemSpace;
		}

		/**
		 * 
		 * @param value
		 */
		public function set itemSpace(value:Number):void
		{
			_itemSpace = value;
			formatList();
		}

		/**
		 * 
		 * @return 
		 */
		public function get bgSkin():String
		{
			return _bgSkin;
		}

		/**
		 * 
		 * @param value
		 */
		public function set bgSkin(value:String):void
		{
			_bgSkin = value;
			drawBG(-1);
		}

		public function get needSelectBg():Boolean
		{
			return _needSelectBg;
		}

		public function set needSelectBg(value:Boolean):void
		{
			_needSelectBg = value;
		}

		public function get itemContainer():Sprite
		{
			return _itemContainer;
		}
		
		public function set autoWidth(val:Boolean):void
		{
			_autoWidth	=	val;
			formatList();
		}
		
		/**
		 * 复位宽度 
		 * 
		 */
		public function reSet():void
		{
			_maxWidth	=	_fixedWidth;
			_scrollPanel.resize(new Rectangle(0,0,_fixedWidth,_fixedHeight));
		}
		/**
		 * 滚动条总在最下面 
		 * 
		 */
		public function updata():void
		{
			_scrollPanel.update(1);
		}
	}
}